#ifndef MODBUSHOST_H
#define MODBUSHOST_H
#include <QVariantMap>
#include <QObject>
#include <QModbusRtuSerialMaster>
#include <QSerialPortInfo>
#define MODBUS_HOLD_REG_START_ADDR     ( 0)
#define MODBUS_HOLD_REG_SIZE           (14)
#define MODBUS_INPUT_REG_START_ADDR    (14)
#define MODBUS_INPUT_REG_SIZE          (11)
enum ret_code_t{
    RET_CONNECT_ERR,
    RET_CONNECT_SUCCESS,
    RET_DISCONNECT,
    RET_WRITE_ERR,
    RET_WRITE_SUCCESS

};

class ModbusHost : public QObject
{
    Q_OBJECT
public:
    explicit ModbusHost(QObject *parent = nullptr);
    void modbus_disconnect();

private:
    QString modbus_com;
    QString modbus_baudRate;
    QString modbus_addr;
    QModbusRtuSerialMaster *modbusDevice;
signals:
    void modbus_ret_code(ret_code_t code);
    void modbus_task_sendSearchPort(QStringList port_list);

    void modbus_get_cfg_finish(QVector<QString> cfg_string);
    void modbus_get_env_finish(QVector<QString> cfg_string);

public slots:
    void modbus_connect_slave(QVariantMap port_info);
    void modbus_task_SearchPort();
    void modbus_task_get_cfg();
    void modbus_task_get_env();

    void read_hold_Reply();
    void read_input_Reply();
    void hold_write_reply();
    void modbus_write_hold(qint16 regaddr,qint16 data);
};

#endif // MODBUSHOST_H
